* Set working directory to "PNAS_Replication/" folder
cd "~/Dropbox/PNAS_Replication"
* install mat2txt and mat2tex
** net install mat2tex, from(https://raw.githubusercontent.com/avila/mat2tex/master/)
** ssc install mat2txt

********************************************
******** Table S1: Sampling Design ********
********************************************
** "Sample Size" and "Fresh Sample" columns
*** wave 1 
use "Data/for_dataverse_wave1_nationwide.dta", clear
count
global count_w1 = `r(N)'
global pct_freshsample_w1 = . 
count if focus == 1
global analysis_w1 = `r(N)'
*** wave 2
use "Data/for_dataverse_wave1_nationwide.dta", clear
keep pseudonym
count
merge 1:1 pseudonym using "Data/for_dataverse_wave2_nationwide.dta", keepusing(pseudonym focus)
count if inlist(_merge, 2, 3)
global count_w2 = `r(N)'
count if _merge == 2
global pct_freshsample_w2 = (`r(N)' / ${count_w2}) * 100
count if inlist(_merge, 2, 3) & focus == 1
global analysis_w2 = `r(N)'


*** wave 3
use "Data/for_dataverse_wave2_nationwide.dta", clear
keep pseudonym
count
merge 1:1 pseudonym using "Data/for_dataverse_wave3_nationwide.dta", keepusing(pseudonym focus)
count if inlist(_merge, 2, 3) /* AW: the same as above: previously lines 34-35, now 32-33 */
global count_w3 = `r(N)'
count if _merge == 2
global pct_freshsample_w3 = (`r(N)' / ${count_w3}) * 100
count if inlist(_merge, 2, 3) & focus == 1
global analysis_w3 = `r(N)'

*** wave 4
use "Data/for_dataverse_wave3_nationwide.dta", clear
keep pseudonym
count
merge 1:1 pseudonym using "Data/for_dataverse_wave4_nationwide.dta", keepusing(pseudonym focus)
count if inlist(_merge, 2, 3) /* AW: the same as above: previously lines 50-51, now 48-49 */
global count_w4 = `r(N)'
count if _merge == 2
global pct_freshsample_w4 = (`r(N)' / ${count_w4}) * 100
count if inlist(_merge, 2, 3) & focus == 1
global analysis_w4 = `r(N)'

mat sampling_design = (., ${count_w1}, ${pct_freshsample_w1}, ${analysis_w1}) \ ///
                      (., ${count_w2}, ${pct_freshsample_w2}, ${analysis_w2}) \ ///
					  (., ${count_w3}, ${pct_freshsample_w3}, ${analysis_w3}) \ ///
					  (., ${count_w4}, ${pct_freshsample_w4}, ${analysis_w4}) 
mat rownames sampling_design =  "Wave 1 & 09/05/2016 – 09/23/2016" "Wave 2 & 01/04/2017 – 01/17/2017" "Wave 3 & 06/19/2017 – 06/30/2017" "Wave 4 & 12/07/2017 – 12/22/2017"
mat colnames sampling_design = "Date" "Sample Size" "Fresh Sample" "Sample Size \(Analyze\)"					 
mat2tex using "Results/SI/Table_S1.tex", matrix(sampling_design) replace format(%9.2fc)					  
	
	
	
********************************************************************
********************* Table S2: Attrition Rate ********************* 
********************************************************************
** starting from waves 1-3, proportion retained into waves 2-4
forvalues s=1/3 {
	forvalues i=2/4 {
		use "Data/for_dataverse_wave`s'_nationwide.dta", clear
		keep pseudonym 
		merge 1:1 pseudonym using "Data/for_dataverse_wave`i'_nationwide.dta", keepusing(pseudonym focus)
		count if inlist(_merge, 1, 3)
		global denom_w`s'w`i' = `r(N)'
		count if _merge == 3 
		global num_w`s'w`i' = `r(N)'
		global retained_w`s'w`i' = ${num_w`s'w`i'} / ${denom_w`s'w`i'} * 100
		drop _merge
	}	
}
mat attrition_rate = (100, ${retained_w1w2}, ${retained_w1w3}, ${retained_w1w4}) \ ///
                     (., 100, ${retained_w2w3}, ${retained_w2w4}) \ ///
					 (., ., 100,  ${retained_w3w4}) \ ///
					 (., ., ., 100)
mat rownames attrition_rate = "From Wave 1" "From Wave 2" "From Wave 3" "From Wave 4"
mat colnames attrition_rate = "Until Wave 1" "Until Wave 2" "Until Wave 3" "Until Wave 4"
mat2tex using "Results/SI/Table_S2.tex", matrix(attrition_rate) replace format(%9.1fc)	  



********************************************************************************************************
***** Table S3: Comparison of List Experiment in Wave 1 with Direct Questions in Waves 2, 3, and 4 *****
********************************************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
foreach var of varlist hate_pol_message hate_violence_means hate_justified hate_prevent_settlement {
	gen agree_`var' = `var' >=3
	replace agree_`var'=. if `var' == .
}
bysort wave: ci means agree*
local myvars "hate_pol_message hate_violence_means hate_justified"
forval w=1/4 {
	foreach var of varlist `myvars' {
		if "`var'" == "hate_pol_message" {
			if `w' == 1 {
				reg outcome_list list_treatment if wave == 1, vce(cluster pseudonym) 			
				mat mat_svy = r(table)
				mat tab_`var'_`w' = (mat_svy["b", "list_treatment"], mat_svy["ll", "list_treatment"], mat_svy["ul", "list_treatment"])
				mat colnames tab_`var'_`w' = "`var' - Prop." "`var' - LB" "`var' - UB"
			} 
			else {
				ci means agree_`var' if wave == `w' 
				mat tab_`var'_`w'  = `r(mean)', `r(lb)', `r(ub)'
				mat colnames tab_`var'_`w' = "`var' - Prop." "`var' - LB" "`var' - UB"
			}
		} 
		else if  "`var'" == "hate_violence_means" & `w' == 2 { 
			ci means agree_`var' if wave == `w' & (list_treatment == . | list_treatment == 0)
			mat tab_`var'_`w'  = `r(mean)', `r(lb)', `r(ub)'
			mat colnames tab_`var'_`w' = "`var' - Prop." "`var' - LB" "`var' - UB"
		} 
		else {
			ci means agree_`var' if wave == `w'
			mat tab_`var'_`w' = `r(mean)', `r(lb)', `r(ub)'		
			mat colnames tab_`var'_`w' = "`var' - Prop." "`var' - LB" "`var' - UB"
		}
	}
	mat tab_wave_`w' = (tab_hate_pol_message_`w', tab_hate_violence_means_`w', tab_hate_justified_`w')
	mat rownames tab_wave_`w' = "Wave `w'"
}
mat tab_list_wave_1 = tab_wave_1 \ tab_wave_2 \ tab_wave_3 \ tab_wave_4
matrix list tab_list_wave_1, format(%12.3f)
mat2txt, matrix(tab_list_wave_1) saving("Results/SI/Table_S3") format(%12.3f) replace


*********************************************************
****** Table S4: Direct Answers to Homes in Wave 2 ******
****** Table S5: Direct Answers to Homes in Wave 3 ******
****** Table S6: Direct Answers to Homes in Wave 4 ******
*********************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
foreach var of varlist hate_pol_message hate_violence_means hate_polcondemn hate_justified hate_prevent_settlement {
	gen agree_`var' = `var' >=3
	replace agree_`var'=. if `var' == .
}

***** Wave 1 list experiment
bysort pseudonym: gen mark_list_treatment_wave_1 = list_treatment if wave == 1
bysort pseudonym: egen list_treatment_wave_1 = max(mark_list_treatment_wave_1)
bysort pseudonym: gen mark_outcome_list_wave_1 = outcome_list if wave == 1
bysort pseudonym: egen outcome_list_wave_1 = max(mark_outcome_list_wave_1)

clear matrix
foreach w of numlist 2/4 {
  forvalues g = 0/1 {
    mat one_outcome = .,.
    foreach i of numlist 0/4 {
		forvalues a=0/1 {
			summ outcome_list_wave_1 if list_treatment_wave_1 == `g' & wave == `w' & outcome_list_wave_1 == `i' & agree_hate_pol_message  == `a'
			mat agree_`a' = r(N)		
	    }
      mat one_outcome = one_outcome \ (agree_0, agree_1)
    }
mat one_outcome_`g' = one_outcome
mat colnames one_outcome_`g' = "w`w' a0 t`g'" "w`w' a1 t`g'" 
mat rownames one_outcome_`g' = n_list_outcome 0 1 2 3 4
  }
mat wave_`w' = one_outcome_1, one_outcome_0
}
mat list_vs_direct_message = wave_2, wave_3, wave_4
mat2txt,  matrix(list_vs_direct_message) saving("Results/SI/tabs_S4_S5_S6") format(%9.0fc) replace
* Add to "tabs_S4_S5_S6" sheet in "Tables_S4_S5_S6.xlsx", then tables from paper are in sheets "Table S4", "Table S5", and "Table S6"


****************************************************************
***** Table S7: Chi-squared test for Homes List Experiment *****
****************************************************************
mat chisq_list_wave_1_treat = .,.,.
mat colnames chisq_list_wave_1_treat = "T- N" "T - stat" "T - pval" 
mat chisq_list_wave_1_control = .,.,.
mat colnames chisq_list_wave_1_control = "C - N" "C - stat" "C - pval" 
forvalues w=2/4 {	
	tab outcome_list_wave_1 agree_hate_pol_message if wave == `w' & list_treatment_wave_1 == 1, col chi2 
	mat chisq_list_wave_1_treat = chisq_list_wave_1_treat \ (r(N), r(chi2), r(p))
	tab outcome_list_wave_1 agree_hate_pol_message if wave == `w' & list_treatment_wave_1 == 0, col chi2 
	mat chisq_list_wave_1_control = chisq_list_wave_1_control \ (r(N), r(chi2), r(p))
}
mat chisq_list_wave_1 = chisq_list_wave_1_treat, chisq_list_wave_1_control
mat rownames chisq_list_wave_1 = "pol_message" "wave 2" "wave 3" "wave 4"
mat2tex using "Results/SI/Table_S7", matrix(chisq_list_wave_1) replace format(%9.3fc)


************************************************************
***** Table S8: List Experiment in Wave 2 (Only Means) *****
************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
foreach var of varlist hate_pol_message hate_violence_means hate_polcondemn hate_justified hate_prevent_settlement {
	gen agree_`var' = `var' >=3
	replace agree_`var'=. if `var' == .
}

***** Wave 2 list experiment
bysort pseudonym: gen mark_list_treatment_wave_2 = list_treatment if wave == 2
bysort pseudonym: egen list_treatment_wave_2 = max(mark_list_treatment_wave_2)
bysort pseudonym: gen mark_outcome_list_wave_2 = outcome_list if wave == 2
bysort pseudonym: egen outcome_list_wave_2 = max(mark_outcome_list_wave_2)

local myvars_fig3 "hate_violence_means hate_justified hate_pol_message hate_prevent_settlement"
*clear matrix
foreach var of varlist `myvars_fig3' {
	if "`var'" == "hate_violence_means" {
		** estimate from wave 2 list experiment
		reg outcome_list list_treatment if wave == 2 & list_treatment != ., vce(cluster pseudonym)
		mat mat_reg = r(table)
		ereturn list
		mat tab_list_`var' = (mat_reg["b", "list_treatment"], mat_reg["ll", "list_treatment"], mat_reg["ul", "list_treatment"], mat_reg["se", "list_treatment"], `e(N)'), . 
		** direct proportion from wave 2 
		mat colnames tab_list_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_list_`var'= "`var' (list)"
		ci means agree_`var' if wave == 2 & (list_treatment == . | list_treatment == 0)
		scalar one_diff = `r(mean)'- mat_reg["b", "list_treatment"]
		mat tab_dir_`var' = `r(mean)', `r(lb)', `r(ub)', `r(se)', `r(N)', abs(one_diff)			
		mat colnames tab_dir_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_dir_`var'= "`var' (direct)"
	} 
	else {
		** direct proportion from wave 2
		ci means agree_`var' if wave == 2
		scalar one_diff = `r(mean)'- mat_reg["b", "list_treatment"]
		mat tab_dir_`var' = `r(mean)', `r(lb)', `r(ub)', `r(se)', `r(N)', abs(one_diff)				
		mat colnames tab_dir_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_dir_`var'= "`var' (direct)"
	}
}
	
mat tab_list_wave_2 = tab_list_hate_violence_means \ tab_dir_hate_violence_means \ /*
*/                    tab_dir_hate_justified \ tab_dir_hate_pol_message \ /*
*/                    tab_dir_hate_prevent_settlement
matrix list tab_list_wave_2, format(%12.3f)
mat2txt, matrix(tab_list_wave_2) saving("Results/SI/Table_S8") format(%12.3f) replace


**************************************************************
****** Table S9: Direct Answers to Only Means in Wave 2 ******
****** Table S10: Direct Answers to Only Means in Wave 3 *****
****** Table S11: Direct Answers to Only Means in Wave 4 *****
**************************************************************
clear matrix
foreach w of numlist 1 3 4 {
  forvalues g = 0/1 {
    mat one_outcome = .,.
    foreach i of numlist 0/4 {
		forvalues a=0/1 {
			summ outcome_list_wave_2 if list_treatment_wave_2 == `g' & wave == `w' & outcome_list_wave_2 == `i' & agree_hate_violence_means == `a'
			mat agree_`a' = r(N)
			
	    }
      mat one_outcome = one_outcome \ (agree_0, agree_1)
    }
mat one_outcome_`g' = one_outcome
mat colnames one_outcome_`g' = "w`w' a0 t`g'" "w`w' a1 t`g'" 
mat rownames one_outcome_`g' = n_list_outcome 0 1 2 3 4
  }
mat wave_`w' = one_outcome_1, one_outcome_0
}
mat list_vs_direct_means = wave_1, wave_3, wave_4
mat2txt,  matrix(list_vs_direct_means) saving("Results/SI/tabs_S9_S10_S11") format(%9.0fc) replace
* Add to "tabs_S9_S10_S11" sheet in "Tables_S9_S10_S11.xlsx", then tables from paper are in sheets "Table S9", "Table S9", and "Table S11"


**********************************************************************
***** Table S12: Chi-squared test for Only Means List Experiment *****
**********************************************************************
mat chisq_list_wave_2_treat = .,.,.
mat colnames chisq_list_wave_2_treat = "T- N" "T - stat" "T - pval" 
mat chisq_list_wave_2_control = .,.,.
mat colnames chisq_list_wave_2_control = "C - N" "C - stat" "C - pval" 
foreach w of numlist 1 3 4 {
	tab outcome_list_wave_2 agree_hate_violence_means if wave == `w' & list_treatment_wave_2 == 1, col chi2 
	mat chisq_list_wave_2_treat = chisq_list_wave_2_treat \ (r(N), r(chi2), r(p))
	tab outcome_list_wave_2 agree_hate_violence_means if wave == `w' & list_treatment_wave_2 == 0, col chi2 
	mat chisq_list_wave_2_control = chisq_list_wave_2_control \ (r(N), r(chi2), r(p))
}

mat chisq_list_wave_2 = chisq_list_wave_2_treat, chisq_list_wave_2_control
mat rownames chisq_list_wave_2 = "violence_means" "wave 1" "wave 3" "wave 4"
mat2tex using "Results/SI/Table_S12", matrix(chisq_list_wave_2) replace format(%9.3fc)

************************************************************************
****** Figure S1: Principal Component Analysis – Hate Crime Items ******
************************************************************************
forvalues w=2/4 {
	use "Data/for_dataverse.dta", clear
	keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
	keep if focus == 1

	gen justified = hate_justified
	gen message = hate_pol_message
	gen prevent = hate_prevent_settlement
	gen means = hate_violence_means
	
	pca justified message prevent means if wave == `w'
	** first PC explains ~82% of variance, second PC explains ~11%
	** plot loadings for first two PCs
	loadingplot, /*
		*/ mcolor(black) msize(small) mlabsize(small) mlabcolor(black%60) /*
	*/ title("PC1 and PC2 Loadings -- 4 Hate Crime Statements", size(medium))
	graph export "Results/SI/Figure_S1_loadings_wave`w'.pdf", replace

	** use pcacoefsave package to save PCA results in separate dataset more conducive to plotting 
	pcacoefsave using "Results/SI/intermediate_files/pca_hate4_wave`w'_unweighted", replace
	** switch to dataset for PCA results
	use "Results/SI/intermediate_files/pca_hate4_wave`w'_unweighted.dta", clear
	** compute percentage of variance explained by each component
	bysort varname: gen sum_eigenvalues=sum(eigenvalue)
	bysort varname: gen prop_var_explained = eigenvalue/sum_eigenvalues[_N]
	twoway connected prop_var_explained PC, sort /*
		*/ graphregion(color(white)) mcolor(black) /*
		*/ xlabel(1(1)4)  xscale(range(1 4)) ylabel(0(0.2)1)  yscale(range(0 1))/*
		*/ xtitle("Principal Component") /*
		*/ ytitle("Proportion of Variance Explained") /*
	*/ title("PCA -- 4 Hate Crime Statements", size(medium))
	graph export "Results/SI/Figure_S1_var_explained_wave`w'.pdf", replace
}


************************************************************************
****** Table S13: Hate Crime Support, Percent (waves 2, 3, and 4) ****** 
************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
keep if focus == 1
foreach var in "violence_means" "justified" "pol_message" "prevent_settlement"  {
	count if hate_`var' != . 
	global denom_prop = `r(N)'
	forvalues i=1/4 {
		count if hate_`var' == `i'
		global `var'_`i' = (`r(N)' / ${denom_prop}) * 100
	}
}
mat hate_levels = (${violence_means_4}, ${violence_means_3}, ${violence_means_2}, ${violence_means_1}) \ ///
                  (${justified_4}, ${justified_3}, ${justified_2}, ${justified_1}) \ ///
                  (${pol_message_4}, ${pol_message_3}, ${pol_message_2}, ${pol_message_1}) \ ///
				  (${prevent_settlement_4}, ${prevent_settlement_3}, ${prevent_settlement_2}, ${prevent_settlement_1})
mat rownames hate_levels = "Only Means" "Justified" "Homes" "Settlement"
mat colnames hate_levels = "Strongly Agree" "Somewhat Agree" "Somewhat Disagree" "Agree"
mat2tex using "Results/SI/Table_S13.tex", matrix(hate_levels) replace format(%9.2fc)				  



************************************************************************
******* Figure S2: Anti-refugee Sentiment and Hate Crime Support *******
************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
keep if focus == 1
*** add value labels to hate crime statements to improve axis labels
*** pol_message		
gen graph_hate_pol_message = hate_pol_message
label define lab_hate_pol_message 1 "Homes: Strongly disagree" 2 "Homes: Somewhat disagree" 3 "Homes: Somewhat agree" 4 "Homes: Strongly agree"
label values graph_hate_pol_message lab_hate_pol_message
*** justified		
gen graph_hate_justified = hate_justified
label define lab_hate_justified 1 "Justified: Strongly disagree" 2 "Justified: Somewhat disagree" 3 "Justified: Somewhat agree" 4 "Justified: Stroangly agree"
label values graph_hate_justified lab_hate_justified
*** violence_means		
gen graph_hate_violence_means = hate_violence_means
label define lab_hate_violence_means 1 "Only Means: Strongly disagree" 2 "Only Means: Somewhat disagree" 3 "Only Means: Somewhat agree" 4 "Only Means: Strongly agree"
label values graph_hate_violence_means lab_hate_violence_means
*** prevent_settlement		
gen graph_hate_prevent_settlement = hate_prevent_settlement
label define lab_hate_prevent_settlement 1 "Settlement: Strongly disagree" 2 "Settlement: Somewhat disagree" 3 "Settlement: Somewhat agree" 4 "Settlement: Strongly agree"
label values graph_hate_prevent_settlement lab_hate_prevent_settlement

*** Figure 2 in PNAS submission, improving axis labels
graph drop _all
hist mean8_ref, by(graph_hate_pol_message, yrescale rows(1) note("")) discrete xtitle("Anti-refugee sentiment") freq name(ref_by1_pol_message, replace)
hist mean8_ref, by(graph_hate_prevent_settlement, yrescale rows(1) note("")) discrete xtitle("Anti-refugee sentiment") freq name(ref_by2_prevent, replace)
hist mean8_ref, by(graph_hate_justified, yrescale rows(1) note("")) discrete xtitle("Anti-refugee sentiment") freq name(ref_by3_justified, replace)
hist mean8_ref, by(graph_hate_violence_means, yrescale rows(1)  note("")) discrete xtitle("Anti-refugee sentiment") freq name(ref_by4_means, replace)

graph dir ref_by*
graph combine `r(list)' , col(1) name(figure_s2, replace) note("") iscale(0.5) xsize(5) ysize(4) 
graph export "Results/SI/Figure_S2.pdf" , replace



**************************************************************************************
***** Table S14: Average age, average years of education, and proportion female, *****
*****            among supporters of hate crime who are pro-refuge  (waves 2-4)  *****
**************************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
keep if focus == 1
*** mark respondents as "atypical" if their mean anti-ref is [1, 2] (one way to say average response to at least some agreement) but response to hate crime statement is 3 or 4
cap drop atypical_1
gen atypical_1 = 0
foreach hatevar in pol_message justified violence_means prevent_settlement {
	replace atypical_1 = 1 if inrange(mean8_ref, 1, 2) & inrange(hate_`hatevar', 3, 4)
}
summ atypical_1
mat prop_atypical_1 = `r(mean)'
mat count_atypical_1 = `r(sum)'
*** mark respondents as "atypical" if their mean anti-ref is [1, 2.5] (another way to say average response to at least some agreement) but response to hate crime statement is 3 or 4
cap drop atypical_2
gen atypical_2 = 0
foreach hatevar in pol_message justified violence_means prevent_settlement {
	replace atypical_2 = 1 if inrange(mean8_ref, 1, 2.5) & inrange(hate_`hatevar', 3, 4)
}
summ atypical_2
mat prop_atypical_2 = `r(mean)'
mat count_atypical_2 = `r(sum)'

*** look at age/education/gender of atypical respondents
foreach predictor in age eduyrs female {
	mat all_`predictor' = .,.,.,.,.,.,.,.
	mat rownames all_`predictor' = "`predictor'"
	mat colnames all_`predictor' = "prop_atypical" "count_atypical" "mean_typical" "mean_atypical" "diff" "CI_diff (lower)" "CI_diff (upper)" "p-value"
	foreach var in atypical_1 atypical_2 {
		reg `predictor' `var',  vce(cluster pseudonym) 
		mat one_`predictor' = prop_`var'[1,1], count_`var'[1, 1], r(table)["b", "_cons"], r(table)["b", "_cons"] + r(table)["b", "`var'"], -1 * r(table)["b", "`var'"], -1 * r(table)["ll", "`var'"], -1 * r(table)["ul", "`var'"], r(table)["pvalue", "`var'"]
		mat rownames one_`predictor' = `var'
		mat all_`predictor' = all_`predictor' \ one_`predictor'
	}
}
mat all_atypical = all_age \ all_eduyrs \ all_female
mat2tex using "Results/SI/Table_S14.tex", matrix(all_atypical) replace format(%9.3fc)

**********************************************************************************************************************
****** Figure S3: X-axis: Average agreement with the 8 anti-refugee statements. Y-axis: Average trust in police ****** 
**********************************************************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
keep if focus == 1
reg tr_police c.mean8_ref##c.mean8_ref, vce(cluster pseudonym)
graph twoway (scatter tr_police mean8_ref, mcolor(gray%30) msize(tiny) jitter(8) xscale(range(0.5 4.2)) yscale(range(0.5 4.2)) ) /*
*/ (lfit tr_police mean8_ref, lwidth(thick) lcolor(black%90)) /*
*/ (qfit tr_police mean8_ref, lwidth(thick) lcolor(red%90)), /*
*/ xtitle("Avg. agreement with the 8 anti-refugee statements (1-4)") /*
*/ ytitle("Trust in police (1-4)") /*
*/ legend(row(1) label(1 trust in police) label(2 linear) label(3 quadratic) region(lstyle(none))) graphregion(color(white))
	
graph export "Results/SI/Figure_S3.pdf", replace
	
********************************************************************************************************************
**** Figure S4: X-axis: Average agreement with the 4 hate-crime statements. Y-axis: Average trust in the police **** 
********************************************************************************************************************
reg tr_police c.mean4_hate##c.mean4_hate, vce(cluster pseudonym)
	graph twoway (scatter tr_police mean4_hate, mcolor(gray%30) msize(tiny) jitter(8) xscale(range(0.5 4.2)) yscale(range(0.5 4.2)) ) /*
*/ (lfit tr_police mean4_hate, lwidth(thick) lcolor(black%90)) /*
*/ (qfit tr_police mean4_hate, lwidth(thick) lcolor(red%90)), /*
*/ xtitle("Avg. agreement with the 4 hate crime statements (1-4)") /*
*/ ytitle("Trust in police (1-4)") /*
*/ legend(row(1) label(1 trust in police) label(2 linear) label(3 quadratic) region(lstyle(none))) 

graph export "Results/SI/Figure_S4.pdf", replace


************************************************
*********** Tables: Marginal Means *************
***** Table S16: AfD Candidate, AfD Voters *****
***** Table S17: AfD Candidate, All Voters *****
***** Table S18: CDU Candidate, CDU Voters *****
***** Table S19: CDU Candidate, All Voters *****
************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if wave==4 

* store independent variables for regression in global macro
global conjafdvars "i.afdv1_fac i.afdv2_fac ib2.afdv3_fac i.afdv4_fac ib2.afdv5_fac i.afdv6_fac ib3.afdv7_fac"
global conjcduvars "i.cduv1_fac i.cduv2_fac ib2.cduv3_fac i.cduv4_fac ib2.cduv5_fac i.cduv6_fac ib2.cduv7_fac"

* loop across parties
local party "afd" "cdu"
foreach v in "own_party" "all" {
	global voters = "`v'"
	foreach p in "`party'" {
		* initialize matrices to store marginal mean results
		foreach i in 0 1 {
			foreach var in mess just viol prev {
				mat one_MM_`p'`i'_`var' = ., ., ., ., ., ., ., ., .
				mat colnames one_MM_`p'`i'_`var' = "N_Clusters" "Estimated_MM" "CI_Lower" "CI_Upper" "SE" "Estimate_Diff" "SE_diff" "t_stat_diff" "p_value_diff"
			}
		}
		mat one_MM_`p'_full = ., ., ., ., ., ., ., ., .
		mat colnames one_MM_`p'_full = "N_Clusters" "Estimated_MM" "CI_Lower" "CI_Upper" "SE" "Estimate_Diff" "SE_diff" "t_stat_diff" "p_value_diff"

		* set restriction to own-party voters, if applicable
		if "$voters" == "own_party" {
			global first_condition = "(`p'_first==1 | `p'_second==1) &"
		}
		else {
			global first_condition = ""
		}
		* perform analysis
		eststo clear

		** full sample
		if "$voters" == "own_party"  {
			qui reg choice`p' ${conj`p'vars} if (`p'_first==1 | `p'_second==1), vce(cluster pseudonym)
			eststo main_`p'_full
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)
		} 
		else {
			qui reg choice`p' ${conj`p'vars}, vce(cluster pseudonym)
			eststo main_`p'_full
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)			
		}

		* marginal means 
		** estimates for both levels of "gun violence" attribute	
		mat one_MM_N = e(N_clust)
		mat one_MM_estimate_yes = r(table)["b", "1.`p'v5_fac"]
		mat one_MM_estimate_no = r(table)["b", "2.`p'v5_fac"]	  
		mat one_MM_CI_lower_yes = r(table)["ll", "1.`p'v5_fac"]
		mat one_MM_CI_lower_no = r(table)["ll", "2.`p'v5_fac"]
		mat one_MM_CI_upper_yes = r(table)["ul", "1.`p'v5_fac"]
		mat one_MM_CI_upper_no = r(table)["ul", "2.`p'v5_fac"]
		mat one_MM_SE_yes = r(table)["se", "1.`p'v5_fac"]
		mat one_MM_SE_no = r(table)["se", "2.`p'v5_fac"]
		** estimates for difference between marginal means (including p-value)
		scalar df_diff = e(df_r)
		nlcom _b[1.`p'v5_fac]-_b[2.`p'v5_fac], post	 
		scalar est_diff = r(b)[1,1]
		scalar SE_diff = sqrt(r(V)[1, 1])
		scalar t_stat_diff = r(b)[1,1] / SE_diff
		scalar p_value_diff = 2 * (1 - t(df_diff, abs(t_stat_diff)))	
		mat own_t_stat_yes = t_stat_diff 
		mat own_p_value_diff = p_value_diff
		mat one_MM_p_value_yes = p_value_diff  
		mat one_MM_p_value_no = p_value_diff 

		** save in matrix format to use as a table
		mat one_MM_`p'_full = one_MM_`p'_full \ (one_MM_N, one_MM_estimate_yes, one_MM_CI_lower_yes, one_MM_CI_upper_yes, one_MM_SE_yes, est_diff, SE_diff, t_stat_diff, p_value_diff) \ (one_MM_N, one_MM_estimate_no, one_MM_CI_lower_no, one_MM_CI_upper_no, one_MM_SE_no, est_diff, SE_diff, t_stat_diff, p_value_diff) 
		mat rownames one_MM_`p'_full = "`p'_full" "Endorses -- Yes" "Endorses -- No"

		* testing difference in interaction term for gun_violence attribute across trust / do_not_trust subsamples
		if "$voters" == "own_party"  {
			reg choice`p' ${conj`p'vars_interact} if (`p'_first==1 | `p'_second==1), vce(cluster pseudonym)
			eststo police_`p'`i'_full
		} 
		else {
			reg choice`p' ${conj`p'vars_interact}, vce(cluster pseudonym)
			eststo police_`p'`i'_full
		}


		** hate_pol_message
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_pol_message == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_mess
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)

			* marginal means 
			** estimates for both levels of "gun violence" attribute	
			mat one_MM_N = e(N_clust)
			mat one_MM_estimate_yes = r(table)["b", "1.`p'v5_fac"]
			mat one_MM_estimate_no = r(table)["b", "2.`p'v5_fac"]	  
			mat one_MM_CI_lower_yes = r(table)["ll", "1.`p'v5_fac"]
			mat one_MM_CI_lower_no = r(table)["ll", "2.`p'v5_fac"]
			mat one_MM_CI_upper_yes = r(table)["ul", "1.`p'v5_fac"]
			mat one_MM_CI_upper_no = r(table)["ul", "2.`p'v5_fac"]
			mat one_MM_SE_yes = r(table)["se", "1.`p'v5_fac"]
			mat one_MM_SE_no = r(table)["se", "2.`p'v5_fac"]
			** estimates for difference between marginal means (including p-value)
			scalar df_diff = e(df_r)
			nlcom _b[1.`p'v5_fac]-_b[2.`p'v5_fac], post	 
			scalar est_diff = r(b)[1,1]
			scalar SE_diff = sqrt(r(V)[1, 1])
			scalar t_stat_diff = r(b)[1,1] / SE_diff
			scalar p_value_diff = 2 * (1 - t(df_diff, abs(t_stat_diff)))	
			mat own_t_stat_yes = t_stat_diff 
			mat own_p_value_diff = p_value_diff
			mat one_MM_p_value_yes = p_value_diff  
			mat one_MM_p_value_no = p_value_diff  		
			** save in matrix format to use as a table
			mat one_MM_`p'`i'_mess = one_MM_`p'`i'_mess \ (one_MM_N, one_MM_estimate_yes, one_MM_CI_lower_yes, one_MM_CI_upper_yes, one_MM_SE_yes, est_diff, SE_diff, t_stat_diff, p_value_diff) \ (one_MM_N, one_MM_estimate_no, one_MM_CI_lower_no, one_MM_CI_upper_no, one_MM_SE_no, est_diff, SE_diff, t_stat_diff, p_value_diff) 
			if `i' == 1 {
				global row_support "Support"
			} 
			else if `i' == 0 {
				global row_support "No Support"
			}
			mat rownames one_MM_`p'`i'_mess =  "`p' - Homes - ${row_support}"  "Endorses -- Yes" "Endorses -- No"

			* testing difference in interaction term for gun_violence attribute across trust / do_not_trust subsamples
			reg choice`p' ${conj`p'vars_interact} if ${first_condition} dum_hate_pol_message == `i', vce(cluster pseudonym)
			eststo police_`p'`i'_mess
		}

		** hate_justified 
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_justified == `i', vce(cluster pseudonym)
			eststo main_`p'`i'_just
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)

			* marginal means 
			** estimates for both levels of "gun violence" attribute	
			mat one_MM_N = e(N_clust)
			mat one_MM_estimate_yes = r(table)["b", "1.`p'v5_fac"]
			mat one_MM_estimate_no = r(table)["b", "2.`p'v5_fac"]	  
			mat one_MM_CI_lower_yes = r(table)["ll", "1.`p'v5_fac"]
			mat one_MM_CI_lower_no = r(table)["ll", "2.`p'v5_fac"]
			mat one_MM_CI_upper_yes = r(table)["ul", "1.`p'v5_fac"]
			mat one_MM_CI_upper_no = r(table)["ul", "2.`p'v5_fac"]
			mat one_MM_SE_yes = r(table)["se", "1.`p'v5_fac"]
			mat one_MM_SE_no = r(table)["se", "2.`p'v5_fac"]
			** estimates for difference between marginal means (including p-value)
			scalar df_diff = e(df_r)
			nlcom _b[1.`p'v5_fac]-_b[2.`p'v5_fac], post	 
			scalar est_diff = r(b)[1,1]
			scalar SE_diff = sqrt(r(V)[1, 1])
			scalar t_stat_diff = r(b)[1,1] / SE_diff
			scalar p_value_diff = 2 * (1 - t(df_diff, abs(t_stat_diff)))	
			mat own_t_stat_yes = t_stat_diff 
			mat own_p_value_diff = p_value_diff
			mat one_MM_p_value_yes = p_value_diff  
			mat one_MM_p_value_no = p_value_diff  		
			** save in matrix format to use as a table
			mat one_MM_`p'`i'_just = one_MM_`p'`i'_just \ (one_MM_N, one_MM_estimate_yes, one_MM_CI_lower_yes, one_MM_CI_upper_yes, one_MM_SE_yes, est_diff, SE_diff, t_stat_diff, p_value_diff) \ (one_MM_N, one_MM_estimate_no, one_MM_CI_lower_no, one_MM_CI_upper_no, one_MM_SE_no, est_diff, SE_diff, t_stat_diff, p_value_diff) 
			if `i' == 1 {
				global row_support "Support"
			} 
			else if `i' == 0 {
				global row_support "No Support"
			}
			mat rownames one_MM_`p'`i'_just = "`p' - Justified - ${row_support}" "Endorses -- Yes" "Endorses -- No"

			* testing difference in interaction term for gun_violence attribute across trust / do_not_trust subsamples
			reg choice`p' ${conj`p'vars_interact} if ${first_condition} dum_hate_justified == `i', vce(cluster pseudonym)
			eststo police_`p'`i'_just
		}

		** hate_violence
		foreach i in 0 1 {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_violence == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_viol
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)		

			* marginal means 
			** estimates for both levels of "gun violence" attribute
			mat one_MM_N = e(N_clust)
			mat one_MM_estimate_yes = r(table)["b", "1.`p'v5_fac"]
			mat one_MM_estimate_no = r(table)["b", "2.`p'v5_fac"]	  
			mat one_MM_CI_lower_yes = r(table)["ll", "1.`p'v5_fac"]
			mat one_MM_CI_lower_no = r(table)["ll", "2.`p'v5_fac"]
			mat one_MM_CI_upper_yes = r(table)["ul", "1.`p'v5_fac"]
			mat one_MM_CI_upper_no = r(table)["ul", "2.`p'v5_fac"]
			mat one_MM_SE_yes = r(table)["se", "1.`p'v5_fac"]
			mat one_MM_SE_no = r(table)["se", "2.`p'v5_fac"]
			** estimates for difference between marginal means (including p-value)
			scalar df_diff = e(df_r)
			nlcom _b[1.`p'v5_fac]-_b[2.`p'v5_fac], post	 
			scalar est_diff = r(b)[1,1]
			scalar SE_diff = sqrt(r(V)[1, 1])
			scalar t_stat_diff = r(b)[1,1] / SE_diff
			scalar p_value_diff = 2 * (1 - t(df_diff, abs(t_stat_diff)))	
			mat own_t_stat_yes = t_stat_diff 
			mat own_p_value_diff = p_value_diff
			mat one_MM_p_value_yes = p_value_diff  
			mat one_MM_p_value_no = p_value_diff  		
			** save in matrix format to use as a table
			mat one_MM_`p'`i'_viol = one_MM_`p'`i'_viol \ (one_MM_N, one_MM_estimate_yes, one_MM_CI_lower_yes, one_MM_CI_upper_yes, one_MM_SE_yes, est_diff, SE_diff, t_stat_diff, p_value_diff) \ (one_MM_N, one_MM_estimate_no, one_MM_CI_lower_no, one_MM_CI_upper_no, one_MM_SE_no, est_diff, SE_diff, t_stat_diff, p_value_diff) 
			if `i' == 1 {
				global row_support "Support"
			} 
			else if `i' == 0 {
				global row_support "No Support"
			}
			mat rownames one_MM_`p'`i'_viol = "`p' - Only Means - ${row_support}" "Endorses -- Yes" "Endorses -- No"

			* testing difference in interaction term for gun_violence attribute across trust / do_not_trust subsamples
			reg choice`p' ${conj`p'vars_interact} if ${first_condition} dum_hate_violence == `i', vce(cluster pseudonym)
			eststo police_`p'`i'_viol

		}

		**hate_prevent
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_prevent == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_prev
			qui margins 1.`p'v5_fac 2.`p'v5_fac, post vce(unconditional)

			* marginal means 
			** estimates for both levels of "gun violence" attribute
			mat one_MM_N = e(N_clust)
			mat one_MM_estimate_yes = r(table)["b", "1.`p'v5_fac"]
			mat one_MM_estimate_no = r(table)["b", "2.`p'v5_fac"]	  
			mat one_MM_CI_lower_yes = r(table)["ll", "1.`p'v5_fac"]
			mat one_MM_CI_lower_no = r(table)["ll", "2.`p'v5_fac"]
			mat one_MM_CI_upper_yes = r(table)["ul", "1.`p'v5_fac"]
			mat one_MM_CI_upper_no = r(table)["ul", "2.`p'v5_fac"]
			mat one_MM_SE_yes = r(table)["se", "1.`p'v5_fac"]
			mat one_MM_SE_no = r(table)["se", "2.`p'v5_fac"]
			** estimates for difference between marginal means (including p-value)
			scalar df_diff = e(df_r)
			nlcom _b[1.`p'v5_fac]-_b[2.`p'v5_fac], post	 
			scalar est_diff = r(b)[1,1]
			scalar SE_diff = sqrt(r(V)[1, 1])
			scalar t_stat_diff = r(b)[1,1] / SE_diff
			scalar p_value_diff = 2 * (1 - t(df_diff, abs(t_stat_diff)))	
			mat own_t_stat_yes = t_stat_diff 
			mat own_p_value_diff = p_value_diff
			mat one_MM_p_value_yes = p_value_diff  
			mat one_MM_p_value_no = p_value_diff  		
			** save in matrix format to use as a table
			mat one_MM_`p'`i'_prev = one_MM_`p'`i'_prev \ (one_MM_N, one_MM_estimate_yes, one_MM_CI_lower_yes, one_MM_CI_upper_yes, one_MM_SE_yes, est_diff, SE_diff, t_stat_diff, p_value_diff) \ (one_MM_N, one_MM_estimate_no, one_MM_CI_lower_no, one_MM_CI_upper_no, one_MM_SE_no, est_diff, SE_diff, t_stat_diff, p_value_diff) 
			if `i' == 1 {
				global row_support "Support"
			} 
			else if `i' == 0 {
				global row_support "No Support"
			}
			mat rownames one_MM_`p'`i'_prev = "`p' - Settlement - ${row_support}" "Endorses -- Yes" "Endorses -- No"

			* testing difference in interaction term for gun_violence attribute across trust / do_not_trust subsamples
			reg choice`p' ${conj`p'vars_interact} if ${first_condition} dum_hate_prevent == `i', vce(cluster pseudonym)
			eststo police_`p'`i'_prev

		}

		* format marginal means in tables	
		mat one_MM_all = one_MM_`p'1_mess \ one_MM_`p'0_mess \ ///
			one_MM_`p'1_just \ one_MM_`p'0_just \ ///
			one_MM_`p'1_viol \ one_MM_`p'0_viol \ ///
			one_MM_`p'1_prev \ one_MM_`p'0_prev

		* save results
		if "`p'" == "afd" & "$voters" == "own_party" {
			global table_number = "S16"
		}
		else if "`p'" == "afd" & "$voters" == "all" {
			global table_number = "S17"
		}
		else if "`p'" == "cdu" & "$voters" == "own_party" {
			global table_number = "S18"
		}
		else if "`p'" == "cdu" & "$voters" == "all" {
			global table_number = "S19"
		}
		else {
			global table_number = "`p'_${voters}"
		}
		mat2tex using "Results/SI/Table_${table_number}.tex", matrix(one_MM_all) replace format(%9.3fc)
	}
}



**************************************************************************************
***** Table S20: Proportion of individuals choosing the AfD or CDU candidate. *****
**************************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if wave==4 
* "All*" column -- sample proportions
mat prop_own_h0 = .
mat prop_own_h1 = .
mat prop_all_h0 = .
mat prop_all_h1 = .
foreach party in afd cdu {
	foreach hvar in pol_message justified violence_means prevent_settlement {
		forvalues i=0/1 {
			summ choice`party' if dum_hate_`hvar' == `i' & (`party'_first == 1 | `party'_second == 1)
			mat one_own_h`i' = `r(mean)'
			mat rownames one_own_h`i'= `party'_`hvar'		
			summ choice`party' if dum_hate_`hvar' == `i' 
			mat one_all_h`i' = `r(mean)'
			mat rownames one_all_h`i' = `party'_`hvar'	
		}
		mat prop_own_h0 = prop_own_h0 \ one_own_h0
		mat prop_own_h1 = prop_own_h1 \ one_own_h1
		
		mat prop_all_h0 = prop_all_h0 \ one_all_h0
		mat prop_all_h1 = prop_all_h1 \ one_all_h1
	}
}
mat prop_neteffects = prop_own_h0, prop_own_h1, prop_all_h0, prop_all_h1
mat colnames prop_neteffects = "own-party -- no support" "own-party -- support" "all -- no support" "all -- support"
mat2tex using "Results/SI/TableS20_Column_All.tex", matrix(prop_neteffects) replace format(%9.3fc)

***************************************************************************
***** Table S21: List Experiment in wave 2 (Only Means). Weighted Data ****
***************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
svyset [pw=weight]
global weighted_suffix "weighted"

foreach var of varlist hate_pol_message hate_violence_means hate_justified hate_prevent_settlement {
	gen agree_`var' = `var' >=3
	replace agree_`var'=. if `var' == .
}

local myvars_fig3 "hate_violence_means hate_justified hate_pol_message hate_prevent_settlement"
*clear matrix
foreach var of varlist `myvars_fig3' {
	if "`var'" == "hate_violence_means" {
		** estimate from wave 2 list experiment
		reg outcome_list list_treatment [pw=weight] if wave == 2 & list_treatment != ., vce(cluster pseudonym)  
		mat mat_reg = r(table)
		ereturn list
		mat tab_list_`var' = (mat_reg["b", "list_treatment"], mat_reg["ll", "list_treatment"], mat_reg["ul", "list_treatment"], mat_reg["se", "list_treatment"], `e(N)'), . 
		** direct proportion from wave 2 
		mat colnames tab_list_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_list_`var'= "`var' (list)"
		svy: mean agree_`var' if wave == 2 & (list_treatment == . | list_treatment == 0)
		mat mat_svy = r(table)	
		scalar one_diff = mat_svy["b", 1] - mat_reg["b", "list_treatment"]
		mat mat_N = e(_N)
		mat tab_dir_`var' = mat_svy["b", 1], mat_svy["ll", 1], mat_svy["ul", 1], mat_svy["se", 1], mat_N[1, 1], abs(one_diff)			
		mat colnames tab_dir_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_dir_`var'= "`var' (direct)"
	} 
	else {
		** direct proportion from wave 2
		svy: mean agree_`var' if wave == 2 
		mat mat_svy = r(table)	
		scalar one_diff = mat_svy["b", 1] - mat_reg["b", "list_treatment"]
		mat mat_N = e(_N)
		mat tab_dir_`var' = mat_svy["b", 1], mat_svy["ll", 1], mat_svy["ul", 1], mat_svy["se", 1], mat_N[1, 1], abs(one_diff)
		
		
		mat colnames tab_dir_`var' = "Proportion" "Proportion - LB" "Proportion - UB" "Proportion - SE" "N" "Abs Diff"
		mat rownames tab_dir_`var'= "`var' (direct)"
	}
}
	
mat tab_list_wave_2 = tab_list_hate_violence_means \ tab_dir_hate_violence_means \ /*
*/                    tab_dir_hate_justified \ tab_dir_hate_pol_message \ /*
*/                    tab_dir_hate_prevent_settlement
matrix list tab_list_wave_2, format(%12.3f)
mat2txt, matrix(tab_list_wave_2) saving("Results/SI/Table_S21") format(%12.3f) replace


*********************************************************************************
**** Figure S5: Anti-Refugee Sentiment and Hate Crime Support. Weighted Data ****
*********************************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
svyset pseudonym [pw=weight]
gen weight_int = round(weight*1000)

gen graph_hate_pol_message = hate_pol_message
label define lab_hate_pol_message 1 "Homes: Strongly disagree" 2 "Homes: Somewhat disagree" 3 "Homes: Somewhat agree" 4 "Homes: Strongly agree"
label values graph_hate_pol_message lab_hate_pol_message

hist mean8_ref [fw=weight_int], by(graph_hate_pol_message, yrescale rows(2) ixtitles note("")) discrete xtitle("Anti-refugee sentiment") frac name(ref_by4_message, replace) title("")

graph export "Results/SI/Figure_S5.pdf" , replace

  
*******************************************************************
****** Figure S6: Hate Crime Support by Group. Weighted Data ******
*******************************************************************
use "Data/for_dataverse.dta", clear
keep if inlist(wave, 1, 2, 3) | first_row_wave_4 == 1
keep if focus == 1
svyset pseudonym [pw=weight]
global prefix "svy:"

label def female 1 "Female" 0 "Male"
label val female female

label define age_group 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60+"
label val age_group age_group 

* Gender
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_female = (., ., ., .) \ (., ., ., .) \ (., ., ., .) \ (., ., ., .)  // adding 3 extra blank rows so this matrix can conform with the others (which have 5 levels instead of 2)
	mat colnames m_`var'_female = "`var'_female" "`var'_female_est" "`var'_female_LL" "`var'_female_UL"
	forvalues i=0/1 {
		$prefix mean dum_hate_`var' if female == `i'	
		mat a = r(table)
		mat m_`var'_female = m_`var'_female \ ((1 + `i' - 0.10 + 0.05 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_female = m_pol_message_female, m_prevent_settlement_female, m_justified_female, m_violence_means_female	
	
* Age
   local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_age_group = ., ., ., .
	mat colnames m_`var'_age_group = "`var'_age_group" "`var'_age_group_est" "`var'_age_group_LL" "`var'_age_group_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if age_group == `i'	
		mat a = r(table)
		mat m_`var'_age_group = m_`var'_age_group \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_age_group = m_pol_message_age_group, m_prevent_settlement_age_group, m_justified_age_group, m_violence_means_age_group					
		
*Income
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_income = ., ., ., .
	mat colnames m_`var'_income = "`var'_income" "`var'_income_est" "`var'_income_LL" "`var'_income_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_income == `i'	
		mat a = r(table)
		mat m_`var'_income = m_`var'_income \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_income = m_pol_message_income, m_prevent_settlement_income, m_justified_income, m_violence_means_income		
	
* Education 
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu = ., ., ., .
	mat colnames m_`var'_edu = "`var'_edu" "`var'_edu_est" "`var'_edu_LL" "`var'_edu_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	
		mat a = r(table)
		mat m_`var'_edu = m_`var'_edu \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu = m_pol_message_edu, m_prevent_settlement_edu, m_justified_edu, m_violence_means_edu		
		
*Education by young male
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu_ym = ., ., ., .
	mat colnames m_`var'_edu_ym = "`var'_edu_ym" "`var'_edu_ym_est" "`var'_edu_ym_LL" "`var'_edu_ym_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	& female==0 & age_group<3
		mat a = r(table)
		mat m_`var'_edu_ym = m_`var'_edu_ym \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu_ym = m_pol_message_edu_ym, m_prevent_settlement_edu_ym, m_justified_edu_ym, m_violence_means_edu_ym		
	
*Education by young female
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu_yf = ., ., ., .
	mat colnames m_`var'_edu_yf = "`var'_edu_yf" "`var'_edu_yf_est" "`var'_edu_yf_LL" "`var'_edu_yf_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	& female==1 & age_group<3
		mat a = r(table)
		mat m_`var'_edu_yf = m_`var'_edu_yf \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu_yf = m_pol_message_edu_yf, m_prevent_settlement_edu_yf, m_justified_edu_yf, m_violence_means_edu_yf		
	
* combine all datasets for ci plots
mat m_demographics = m_female, m_age_group, m_income, m_edu, m_edu_ym, m_edu_yf	
drop _all
svmat m_demographics, names(col)
drop if _n == 1	
	
* make ci plots
** female
twoway (scatter pol_message_female_est pol_message_female, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_female_est prevent_settlement_female, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_female_est justified_female, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_female_est violence_means_female, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_female_UL pol_message_female_LL pol_message_female, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_female_UL prevent_settlement_female_LL prevent_settlement_female, lstyle(ci) msize(vtiny))   ///
       (rcap justified_female_UL justified_female_LL justified_female, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_female_UL violence_means_female_LL violence_means_female, lstyle(ci) msize(vtiny)),   ///
       name(plot1_female, replace) ///
	   note("") /// 
       legend(pos(6) ring(0) region(lstyle(none)) col(4) colgap(1pt) keygap(1pt) order(1 "Homes" 2 "Settlement" 3 "Justified" 4 "Means") size(16pt)) ///
       ytitle("Prop. Supporting Hate Crime", height(6)) ylab(.0 .1 .2 .3) xlab(1 "Male" 2 "Female") ///
       xtitle("Gender")
	   
** age_group
twoway (scatter pol_message_age_group_est pol_message_age_group, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_age_group_est prevent_settlement_age_group, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_age_group_est justified_age_group, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_age_group_est violence_means_age_group, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_age_group_UL pol_message_age_group_LL pol_message_age_group, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_age_group_UL prevent_settlement_age_group_LL prevent_settlement_age_group, lstyle(ci) msize(vtiny))   ///
       (rcap justified_age_group_UL justified_age_group_LL justified_age_group, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_age_group_UL violence_means_age_group_LL violence_means_age_group, lstyle(ci) msize(vtiny)),   ///
       name(plot2_age, replace) ///
	   note("") /// 
       legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3) xlab(1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60+") ///
       xtitle("Age Group")
	   
** income
twoway (scatter pol_message_income_est pol_message_income, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_income_est prevent_settlement_income, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_income_est justified_income, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_income_est violence_means_income, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_income_UL pol_message_income_LL pol_message_income, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_income_UL prevent_settlement_income_LL prevent_settlement_income, lstyle(ci) msize(vtiny))   ///
       (rcap justified_income_UL justified_income_LL justified_income, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_income_UL violence_means_income_LL violence_means_income, lstyle(ci) msize(vtiny)),   ///
       name(plot3_income, replace) ///
	   legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3) ///
       xtitle("Household Income (Quintiles)") 

** education
twoway (scatter pol_message_edu_est pol_message_edu, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_est prevent_settlement_edu, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_est justified_edu, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_est violence_means_edu, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_UL pol_message_edu_LL pol_message_edu, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_UL prevent_settlement_edu_LL prevent_settlement_edu, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_UL justified_edu_LL justified_edu, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_UL violence_means_edu_LL violence_means_edu, lstyle(ci) msize(vtiny)),   ///
       name(plot4_edu, replace) ///
	   legend(off) note("")  /// 
       ytitle("Prop. Supporting Hate Crime", height(6)) ylab(.0 .1 .2 .3) ///
       xtitle("Years of Education (Quintiles)") 

** education, young males
twoway (scatter pol_message_edu_ym_est pol_message_edu_ym, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_ym_est prevent_settlement_edu_ym, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_ym_est justified_edu_ym, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_ym_est violence_means_edu_ym, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_ym_UL pol_message_edu_ym_LL pol_message_edu_ym, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_ym_UL prevent_settlement_edu_ym_LL prevent_settlement_edu_ym, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_ym_UL justified_edu_ym_LL justified_edu_ym, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_ym_UL violence_means_edu_ym_LL violence_means_edu_ym, lstyle(ci) msize(vtiny)),   ///
       name(plot5_edu_ym, replace) ///
	   legend(off) note("")  /// 
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3 .4 .5) ///
       xtitle("Years of Education (Quintiles), Men Aged 18-39") 
	   
** education, young females
twoway (scatter pol_message_edu_yf_est pol_message_edu_yf, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_yf_est prevent_settlement_edu_yf, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_yf_est justified_edu_yf, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_yf_est violence_means_edu_yf, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_yf_UL pol_message_edu_yf_LL pol_message_edu_yf, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_yf_UL prevent_settlement_edu_yf_LL prevent_settlement_edu_yf, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_yf_UL justified_edu_yf_LL justified_edu_yf, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_yf_UL violence_means_edu_yf_LL violence_means_edu_yf, lstyle(ci) msize(vtiny)),   ///
       name(plot6_edu_yf, replace) ///
       legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3 .4 .5) ///
       xtitle("Years of Education (Quintiles), Women Aged 18-39")  
	   
graph dir plot*
graph combine `r(list)', col(3) name(demographics_support_w4_6groups, replace) note("") xsize(14) ysize(8)
	   
graph export "Results/SI/Figure_S6.pdf" , replace




***************************************************************************************************
***** Table S22: Effect of Candidates Endorsing Violence on Candidate Support. Weighted Data. *****
***************************************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if wave == 4  

* loop across parties
local party "afd" "cdu"
foreach v in "own_party" "all" {
	global voters = "`v'"
	foreach p in "`party'" {	
		* set restriction to own-party voters, if applicable
		if "$voters" == "own_party" {
			global first_condition = "(`p'_first==1 | `p'_second==1) &"
		}
		else {
			global first_condition = ""
		}
		* perform analysis
		eststo clear

		** full sample
		if "$voters" == "own_party"  {
			qui reg choice`p' ${conj`p'vars} if (`p'_first==1 | `p'_second==1) [pw=weight], vce(cluster pseudonym)
			eststo main_`p'_full
		}
		else {
			qui reg choice`p' ${conj`p'vars} [pw=weight], vce(cluster pseudonym)
			eststo main_`p'_full		
		}

		** hate_pol_message
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_pol_message == `i' [pw=weight], vce(cluster pseudonym)
			eststo  main_`p'`i'_mess
		}

		** hate_justified 
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_justified == `i' [pw=weight], vce(cluster pseudonym)
			eststo main_`p'`i'_just
		}

		** hate_violence
		foreach i in 0 1 {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_violence == `i' [pw=weight], vce(cluster pseudonym)
			eststo  main_`p'`i'_viol	
		}

		**hate_prevent
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_prevent == `i' [pw=weight], vce(cluster pseudonym)
			eststo  main_`p'`i'_prev
		}
		* save results
		esttab main* using "Results/SI/Table_S22_`p'_${voters}.tex", mtitles keep(1.`p'v5_fac) se replace
	}
}
